package com.zy.portal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zy.portal.pojo.SearchResult;
import com.zy.portal.service.SearchService;

/**
 * 商品从solr索引库中搜索
 * @author fys
 *
 */
@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;
	
	@RequestMapping("/search")
	public String search(@RequestParam("q") String queryString,
			@RequestParam(defaultValue="1") Integer page,Model model) {
		if(queryString!=null) {
			try {
				//解决编码问题
				queryString = new String(queryString.getBytes("iso-8859-1"));
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		SearchResult searchResult = searchService.search(queryString, page);
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", searchResult.getPageCount());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("page", page);
		return "search";
	}
}
